/*
 * (C) Copyright 2012, by Tim Xie.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.superapk.diamond.test;
import com.superapk.diamond.R;
import com.superapk.diamond.sqllite.DBManager;
import com.superapk.diamond.sqllite.exception.CreateTableException;
import com.superapk.diamond.sqllite.exception.InsertSqlException;
import com.superapk.diamond.test.entity.User;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * Sample入口
 * @author     Tim Xie [tim.xie2008@gmail.com]
 * @date       2013-2-21
 * 
 */

public class MainListActivity extends ListActivity {
	
    private String[] mStrings = {"NewsView","Panel"};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_mainlist);
		
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, mStrings);
		setListAdapter(adapter);
		
		User user = new User();
		//
		DBManager dbManager = DBManager.getInstance(this);
		try {
			//创建表格
			dbManager.createTable(user);
			
			user.setUserName("tim");
			user.setPassword("123");
			dbManager.saveEntity(user);
			
			user.setUserName("tim123");
			user.setPassword("123456");
			dbManager.saveEntity(user);
			
		} catch (CreateTableException e) {
			e.printStackTrace();
		} catch (InsertSqlException e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		//super.onListItemClick(l, v, position, id);
		switch(position){
			case 0:
				openNewsViewActivity();
				break;
			case 1:
				openPanelActivity();
				break;
		}
	}
	
	private void openNewsViewActivity(){
		Intent intent = new Intent(MainListActivity.this,NewsViewActivity.class);
		startActivity(intent);
	}
	
	private void openPanelActivity(){
		Intent intent = new Intent(MainListActivity.this,PanelActivity.class);
		startActivity(intent);
	}
	
}
